﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace EWDTAssignment
{
    public partial class setPromotion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = App_Code.DBManager.RetrieveCategory();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem items = new ListItem(dt.Rows[i][0].ToString());
                if(PromotionProCategoryDDL.Items.Contains(items))
                {
                    continue;
                }
                else
                {
                    PromotionProCategoryDDL.Items.Add(items);
                }
            }
        }

        protected void PromotionProCategoryDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            string category = PromotionProCategoryDDL.SelectedItem.Text;
            DataTable dt = App_Code.DBManager.retrieveProduct(category);
            PromotionProIdDDL.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem items = new ListItem(dt.Rows[i][2].ToString(), dt.Rows[i][0].ToString());
                PromotionProIdDDL.Items.Add(items);
            }
        }

        protected void PromotionProIdDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            string productID = PromotionProIdDDL.SelectedItem.Value;
            PromotionProIDTbx.Text = productID;
        }

        protected void PromotionSaveBtn_Click(object sender, EventArgs e)
        {
            if (PromotionImgUpload.HasFile)
            {
                string filename = PromotionImgUpload.FileName;
                string path = Server.MapPath("images\\"+filename);
                PromotionImgUpload.SaveAs(path);
                string ImgSrc = "~/images/" + filename;

                double discountRate = Convert.ToDouble(PromotionDiscountRateTbx.Text);
                int productID =Convert.ToInt32(PromotionProIdDDL.SelectedItem.Value);
                DateTime startTime = DateTime.Parse(PromotionStartTimeTbx.Text);
                DateTime endTime = DateTime.Parse(PromotionEndTimeTbx.Text);
                App_Code.DBManager.CreatePromotion(discountRate, startTime, endTime, productID, ImgSrc);
            }
        }
    }
}